/* 
 *  Created By: Mike Stevens
 *  Project URL: http://www.codeplex.com/sitesecuritymgmt
 *  License: GNU GPL v2
 *  Copyright (C) 2008 Mike Stevens
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace SiteSecurityManagement.Entities.SecurityTree
{
    public enum SecurityTreeNodeTypes
    {
        SPSite,
        SPWeb,
        SPList,
        SPDocumentLibrary,
        SPPictureLibrary,
        SPFolder,
        SPFile,
        SPListItem,
        InvalidObjectType
    }

    class SecurityTreeNode
    {
        #region Members
        private string m_siteImage = "~/_layouts/images/ssmu/stsicon.gif";
        private string m_listImage = "~/_layouts/images/ssmu/sts_list_genericlist16.gif";
        private string m_docLibImage = "~/_layouts/images/ssmu/sts_list_documentlibrary16.gif";
        private string m_folderImage = "~/_layouts/images/ssmu/folder.gif";
        private string m_fileImage = "~/_layouts/images/ssmu/icgen.gif";
        private string m_listItemImage = "~/_layouts/images/ssmu/sts_listitem16.gif";

        private string m_siteImageNoInherit = "~/_layouts/images/ssmu/stsicon_NoInherit.gif";
        private string m_listImageNoInherit = "~/_layouts/images/ssmu/sts_list_genericlist16_NoInherit.gif";
        private string m_docLibImageNoInherit = "~/_layouts/images/ssmu/sts_list_documentlibrary16_NoInherit.gif";
        private string m_folderImageNoInherit = "~/_layouts/images/ssmu/folder_NoInherit.gif";
        private string m_fileImageNoInherit = "~/_layouts/images/ssmu/icgen_NoInherit.gif";
        private string m_listItemImageNoInherit = "~/_layouts/images/ssmu/sts_listitem16_NoInherit.gif";

        private const string NAVURLSITE = "{0}/_layouts/ssmu_editSecurity.aspx";
        private const string NAVURLDOCLIBRARY = "{0}/_layouts/ssmu_editSecurity.aspx?obj={{{1}}},doclib&list={{{2}}}";
        private const string NAVURLLIST = "{0}/_layouts/ssmu_editSecurity.aspx?obj={{{1}}},list&list={{{2}}}";
        private const string NAVURLLISTITEM = "{0}/_layouts/ssmu_editSecurity.aspx?obj={{{1}}},{2},LISTITEM&list={{{3}}}";

        private char m_pathSeperator;
        #endregion

        #region Constructor/Destructor
        public SecurityTreeNode(char pathSeperator)
        {
            m_pathSeperator = pathSeperator;
        }
        #endregion

        #region Properties
        #endregion

        #region Methods
        /// <summary>
        /// Gets the path seperator.
        /// </summary>
        /// <returns></returns>
        private char GetPathSeperator()
        {
            return m_pathSeperator;
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="value">The value.</param>
        /// <param name="navUrl">The nav URL.</param>
        /// <param name="imageUrl">The image URL.</param>
        /// <returns>A new TreeNode</returns>
        private TreeNode NewNode(string text, string value, string navUrl, string imageUrl)
        {
            TreeNode node = new TreeNode();
            node.Text = text;
            node.Value = value;
            node.NavigateUrl = navUrl;
            node.ImageUrl=imageUrl;

            return node;
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <param name="inheritsSecurity">if set to <c>true</c> [inherits security].</param>
        /// <param name="navigationUrl">if set to <c>true</c> [navigation URL].</param>
        /// <returns></returns>
        public TreeNode CreateNewNode(SPWeb site, bool inheritsSecurity, bool navigationUrl)
        {
            return this.CreateNewNode(null, site, inheritsSecurity, navigationUrl);
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="siteCollection">The site collection.</param>
        /// <param name="site">The site.</param>
        /// <param name="inheritsSecurity">if set to <c>true</c> [inherits security].</param>
        /// <param name="navigationUrl">if set to <c>true</c> [navigation URL].</param>
        /// <returns></returns>
        public TreeNode CreateNewNode(SPSite siteCollection, SPWeb site, bool inheritsSecurity, bool navigationUrl)
        {
            StringBuilder nodePath = new StringBuilder();
            if (siteCollection!=null)
            {
                nodePath.Append("SPSite" + this.GetPathSeperator());
                nodePath.Append(siteCollection.ID.ToString() + this.GetPathSeperator());
            }
            nodePath.Append(site.GetType().ToString() + this.GetPathSeperator());
            nodePath.Append(site.ID.ToString());

            string url = GetSiteNavUrl(site.Url);

            string img = "";
            if (true == site.IsRootWeb)
            {
                img = m_siteImage;
            }
            else if (false == inheritsSecurity)
            {
                img = m_siteImageNoInherit;
            }
            else
            {
                img = m_siteImage;
            }

            return this.NewNode(site.Title, nodePath.ToString(), url, img);
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <param name="inheritsSecurity">if set to <c>true</c> [inherits security].</param>
        /// <param name="navigationUrl">if set to <c>true</c> [navigation URL].</param>
        /// <returns></returns>
        public TreeNode CreateNewNode(SPList list, bool inheritsSecurity, bool navigationUrl)
        {
            StringBuilder nodePath = new StringBuilder();
            nodePath.Append(list.GetType().ToString() + this.GetPathSeperator());
            nodePath.Append(list.ID.ToString());

            string url = GetListNavUrl(list.ParentWeb.Url, list.ID, SecurityTreeNodeTypes.SPList);

            string img = "";
            if (false == inheritsSecurity)
            {
                img = m_listImageNoInherit;
            }
            else
            {
                img = m_listImage;
            }

            return this.NewNode(list.Title, nodePath.ToString(), url, img);
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="docLib">The doc lib.</param>
        /// <param name="inheritsSecurity">if set to <c>true</c> [inherits security].</param>
        /// <param name="navigationUrl">if set to <c>true</c> [navigation URL].</param>
        /// <returns></returns>
        public TreeNode CreateNewNode(SPDocumentLibrary docLib,bool inheritsSecurity, bool navigationUrl)
        {
            StringBuilder nodePath = new StringBuilder();
            nodePath.Append(docLib.GetType().ToString() + this.GetPathSeperator());
            nodePath.Append(docLib.ID.ToString());

            string url = GetListNavUrl(docLib.ParentWeb.Url, docLib.ID, SecurityTreeNodeTypes.SPDocumentLibrary);

            string img = "";
            if (false == inheritsSecurity)
            {
                img = m_docLibImageNoInherit;
            }
            else
            {
                img = m_docLibImage;
            }

            return this.NewNode(docLib.Title, nodePath.ToString(), url, img);
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="folder">The folder.</param>
        /// <param name="inheritsSecurity">if set to <c>true</c> [inherits security].</param>
        /// <param name="navigationUrl">if set to <c>true</c> [navigation URL].</param>
        /// <returns></returns>
        public TreeNode CreateNewNode(SPFolder folder, bool inheritsSecurity, bool navigationUrl)
        {
            StringBuilder nodePath = new StringBuilder();
            nodePath.Append(folder.GetType().ToString() + this.GetPathSeperator());
            nodePath.Append(folder.UniqueId.ToString());

            string url = GetListItemNavUrl(folder.ParentWeb.Url, folder.ParentListId,folder.Item.ID);

            string img = "";
            if (false == inheritsSecurity)
            {
                img = m_folderImageNoInherit;
            }
            else
            {
                img = m_folderImage;
            }

            return this.NewNode(folder.Name, nodePath.ToString(), url, img);
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="listItem">The list item.</param>
        /// <param name="inheritsSecurity">if set to <c>true</c> [inherits security].</param>
        /// <param name="navigationUrl">if set to <c>true</c> [navigation URL].</param>
        /// <returns></returns>
        public TreeNode CreateNewNode(SPListItem listItem, bool inheritsSecurity, bool navigationUrl)
        {
            StringBuilder nodePath = new StringBuilder();
            nodePath.Append(listItem.GetType().ToString() + this.GetPathSeperator());
            nodePath.Append(listItem.UniqueId.ToString());

            string url = GetListItemNavUrl(listItem.ParentList.ParentWeb.Url, listItem.ParentList.ID, listItem.ID);

            string img = "";
            if (false == inheritsSecurity)
            {
                img = m_listItemImageNoInherit;
            }
            else
            {
                img = m_listItemImage;
            }

            return this.NewNode(listItem.Title, nodePath.ToString(), url, img);
        }

        /// <summary>
        /// Creates a new node.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="inheritsSecurity">if set to <c>true</c> [inherits security].</param>
        /// <param name="navigationUrl">if set to <c>true</c> [navigation URL].</param>
        /// <returns></returns>
        public TreeNode CreateNewNode(SPFile file, bool inheritsSecurity, bool navigationUrl)
        {
            StringBuilder nodePath = new StringBuilder();
            nodePath.Append(file.GetType().ToString() + this.GetPathSeperator());
            nodePath.Append(file.UniqueId.ToString());

            string url = GetListItemNavUrl(file.ParentFolder.ParentWeb.Url, file.ParentFolder.ParentListId, file.Item.ID);

            string img = "";
            if (false == inheritsSecurity)
            {
                img = m_fileImageNoInherit;
            }
            else
            {
                img = m_fileImage;
            }

            return this.NewNode(file.Name, nodePath.ToString(), url, img);
        }

        /// <summary>
        /// Gets the site nav URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <returns></returns>
        protected string GetSiteNavUrl(string siteUrl)
        {
            string url=String.Format(NAVURLSITE,siteUrl);
            return new Uri(url, UriKind.RelativeOrAbsolute).ToString();
        }

        /// <summary>
        /// Gets the list nav URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="listId">The list id.</param>
        /// <param name="listType">Type of the list.</param>
        /// <returns></returns>
        protected string GetListNavUrl(string siteUrl, Guid listId, SecurityTreeNodeTypes listType)
        {
            string url = "";
            switch (listType)
            {
                case SecurityTreeNodeTypes.SPList:
                    
                    url = String.Format(NAVURLLIST,siteUrl,listId.ToString(),listId.ToString());
                    break;
                case SecurityTreeNodeTypes.SPDocumentLibrary:
                    url = String.Format(NAVURLDOCLIBRARY, siteUrl, listId.ToString(), listId.ToString());
                    break;
                default:
                    url = String.Format(NAVURLLIST, siteUrl, listId.ToString(), listId.ToString());
                    break;
            }
            return new Uri(url, UriKind.RelativeOrAbsolute).ToString();
        }

        /// <summary>
        /// Gets the list item nav URL.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="listId">The list id.</param>
        /// <param name="listItemId">The list item id.</param>
        /// <returns></returns>
        protected string GetListItemNavUrl(string siteUrl, Guid listId, int listItemId)
        {
            string url = String.Format(NAVURLLISTITEM,siteUrl,listId.ToString(),listItemId.ToString(),listId.ToString());
            return new Uri(url, UriKind.RelativeOrAbsolute).ToString();
        }
        #endregion
    }
}
